﻿@model Jb.Web.ViewModels.JobViewModel
@using Jb.Common.Extensions;
@using Jb.Web.Utility;
@using Jb.Web.ViewModels;
@using Jb.Domain;
@{
    String pageTitle = Model != null && Model.ID == 0 ? "Add New Job" : "Edit Job";    
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = EmployerMenu.ManageJobs;

    bool _HasValidationError = ViewData[AppConstants.ViewDataKeys.HAS_VALIDATION_ERROR] != null ? true : false;
    bool _IsSaved = TempData[AppConstants.ViewDataKeys.SAVED] == null ? false : TempData[AppConstants.ViewDataKeys.SAVED].ToString().ToBoolean();    
    List<Jb.Web.ViewModels.CategoryViewModel> _Categories = ApplicationCache.Categories;
    String _CategoryButtonText = "Select Category";
    //Jb.Domain.JbEntities _Context = new Jb.Domain.JbEntities();
    //int categoryID = 4;
    //List<SkillViewModel> skills = AutoMapper.Mapper.Map<List<Skill>, List<SkillViewModel>>(_Context.Skills.Where(P => P.CategoryID == categoryID).ToList());
    //HtmlString _SkillsJson = new HtmlString(skills.ToJSON());
    //List<EducationViewModel> educations = AutoMapper.Mapper.Map<List<Education>, List<EducationViewModel>>(_Context.Educations.ToList());
    //HtmlString _EducationsJson = new HtmlString(educations.ToJSON());
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = EmployerMenu.ManageJobs;
}

<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>Enter necessary information and then click the Save button.</div>
</div>

<div class="jb-page-content">
@if (!PackageInfo.CanPostJob && Model.ID <= 0) { //Users can edit job, but cannot add a new one before subscription
    @Html.WarningMessageBox("You need to subscribe to a package to post jobs. <a href='/Package/List'>Click here</a> to subscribe to a package.");
}
else
{
    using (Html.BeginForm())
    {
    @Html.ValidationSummary(false) //Made it "false" intentionally, because it is hardly visible the field for a server side validation failure

        if (Model == null)
        {
            @: @Html.WarningMessageBox("Sorry! Requested Job was not found for edit.")
        }
        else
        {
            String detailsUrl = Url.JobDetailsUrl(Model.ID, Model.Url);
            if (Model.ID > 0)
            {
                _CategoryButtonText = "Change Category";
            }
            if (_IsSaved)
            {
            @: @Html.SuccessMessageBox(String.Format("Job Saved Successfully. <a href='{1}' target='_blank' class='btn btn-mini pull-right'>Preview</a> <a href='{0}' class='btn btn-primary btn-mini pull-right' style='margin-right:5px;'>Add Another Job</a>", "/Private/Job/Edit", detailsUrl))
        }
	    @Html.HiddenFor(model => model.ID)
        <div class="jb-group-title">Basic Information</div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.CategoryID)
	    </div>            
	    <div class="editor-field">			
            <span id="spanSelectedCategory">
            @if (Model.CategoryID > 0)
            {
                @Html.GetCategoryBreadCrumb(Model.CategoryID, true)
            }
            </span>
            <button id="btnCategorySelection" class="btn" type="button"><i class="icon-edit"></i> @(_CategoryButtonText)</button>
            @Html.HiddenFor(model => model.CategoryID)
		    @Html.ValidationMessageFor(model => model.CategoryID)
            @Html.HiddenFor(model => model.CompanyID)
	    </div>
    
	    <div class="editor-label">
		    @Html.LabelFor(model => model.Title)
	    </div>
	    <div class="editor-field">
		    @Html.TextBoxFor(model => model.Title, new { @class = "jb-long-text" })
		    @Html.ValidationMessageFor(model => model.Title)
	    </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.Description)
	    </div>
        <div class="jb-label-hint">
            If you want to create paragraph in your Job Description then, hit the "Enter" button 
            in your keyboard to start a new line, then start typing a new paragraph.
        </div>
	    <div class="editor-field">
		    @Html.TextAreaFor(model => model.Description, new { @class = "jb-long-text" })
		    @Html.ValidationMessageFor(model => model.Description)
	    </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.EmployeeTypeID)
	    </div>
	    <div class="editor-field">
		    @Html.DropDownListFor(model => model.EmployeeTypeID, BindSelectList.EmployeeType())
		    @Html.ValidationMessageFor(model => model.EmployeeTypeID)
	    </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.JobLevelID)
	    </div>
	    <div class="editor-field">
		    @Html.DropDownListFor(model => model.JobLevelID, BindSelectList.JobLevels())
		    @Html.ValidationMessageFor(model => model.JobLevelID)
	    </div>
    
        <div class="jb-group-title">Job Location</div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.CountryID)
	    </div>
	    <div class="editor-field">
		    @Html.DropDownListFor(model => model.CountryID, BindSelectList.Countries())
		    @Html.ValidationMessageFor(model => model.CountryID)
	    </div>    
        <div class="editor-label">
		    @Html.LabelFor(model => model.StateID)
	    </div>
	    <div class="editor-field">
		    @Html.DropDownListFor(model => model.StateID, BindSelectList.States(Model.CountryID))
		    @Html.ValidationMessageFor(model => model.StateID)
	    </div>    
	    <div class="editor-label">
		    @Html.LabelFor(model => model.CityID)
	    </div>
	    <div class="editor-field">
		    @Html.DropDownListFor(model => model.CityID, BindSelectList.CitiesByState(Model.StateID))
		    @Html.ValidationMessageFor(model => model.CityID)
	    </div>
        <div class="jb-group-title">Salary Information</div>
        <div class="jb-label-hint">
            (If you don't want to mention salary, then just leave it blank, in the job listing it will be displayed as "Negotiable".
            In that case users will not be able to filter your job with salary range.)
        </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.SalaryRangeFrom)            
	    </div>
	    <div class="editor-field">
		    @Html.EditorFor(model => model.SalaryRangeFrom)
		    @Html.ValidationMessageFor(model => model.SalaryRangeFrom)
	    </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.SalaryRangeTo)
	    </div>
	    <div class="editor-field">
		    @Html.EditorFor(model => model.SalaryRangeTo)
		    @Html.ValidationMessageFor(model => model.SalaryRangeTo)
	    </div>
        <div class="jb-group-title">Educational Qualifications</div>
        <div class="editor-label jb-inline-label">
		    Select @Html.LabelFor(model => model.Educations) (If applicable)
	    </div>
	    <div class="editor-field">
            <div class="float-left">
		        @Html.ListBoxFor(model => model.Educations, BindSelectList.Educations())
		        @Html.ValidationMessageFor(model => model.Educations)                
            </div>
            <div class="float-left" style="margin-left:10px;">                
                <button class="btn" onclick="ClearSelection('Educations');"><i class="icon-refresh"></i> Clear Selection</button>
            </div>
            <div class="clear"></div>
	    </div>
        <div class="editor-label">Your Own Preferred Education</div>
        <div class="jb-label-hint">
            You can give your own educational preferences here. Put each education in a new line. After typing an education hit the "Enter" button 
            of your keyboard, it will produce a new line to separate them from each other.
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.OtherEducation, new { @class = "jb-long-text" })            
        </div>
    
        <div class="jb-group-title">Special Information</div>
    
        <div class="editor-label">@Html.LabelFor(model => model.Skills)</div>
        <div class="editor-field">
            <div class="float-left">
		        @Html.ListBoxFor(model => model.Skills, BindSelectList.Skills(Model.CategoryID))
		        @Html.ValidationMessageFor(model => model.Skills)                
            </div>
            <div class="float-left" style="margin-left:10px;">                
                <button class="btn" onclick="ClearSelection('Skills');"><i class="icon-refresh"></i> Clear Selection</button>
            </div>
            <div class="clear"></div>
	    </div>
    
        <div class="editor-label">@Html.LabelFor(model => model.OtherSkill)</div>
        <div class="jb-label-hint">
            You can give your own skill preferences here. Put each skill in a new line. Hit the "Enter" button 
            in your keyboard after typing a skill, it will produce a new line to separate them from each other.
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.OtherSkill, new { @class = "jb-long-text" })            
        </div>    
        
        <div class="editor-label">What we have to offer?</div>
        <div class="jb-label-hint">Every New Line will produce a bulleted list in the job details page.</div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.SpecialOffer, new { @class = "jb-long-text" })
        </div>
        
        <div class="jb-group-title">Job Settings</div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.PostDate)
	    </div>
	    <div class="editor-field">
		    @Html.TextBoxFor(model => model.PostDate, new { @class = "datepicker" })
		    @Html.ValidationMessageFor(model => model.PostDate)
	    </div>
	    <div class="editor-label">
		    @Html.LabelFor(model => model.EndDate)
	    </div>
	    <div class="editor-field">
		    @Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker" })
		    @Html.ValidationMessageFor(model => model.EndDate)
	    </div>
	    <div class="editor-label">
		    @Html.LabelFor(model => model.SendExpireAlert)
	    </div>
	    <div class="editor-field jb-inline-label">
		    @Html.RadioButtonFor(model => model.SendExpireAlert, "true", new { id = "rdnExpireAlert_1" }) <label for="rdnExpireAlert_1">Yes</label>
            @Html.RadioButtonFor(model => model.SendExpireAlert, "false", new { id = "rdnExpireAlert_0" }) <label for="rdnExpireAlert_0">No</label>
		    @Html.ValidationMessageFor(model => model.SendExpireAlert)
	    </div>
        <div class="editor-label">
		    @Html.LabelFor(model => model.IsActive)
	    </div>
	    <div class="editor-field  jb-inline-label">
		    @Html.RadioButtonFor(model => model.IsActive, "true", new { id = "rdnIsActive_1" }) <label for="rdnIsActive_1">Yes</label>
            @Html.RadioButtonFor(model => model.IsActive, "false", new { id = "rdnIsActive_0" }) <label for="rdnIsActive_0">No</label>
		    @Html.ValidationMessageFor(model => model.IsActive)
	    </div>
	    
            if (_HasValidationError)
            {
            @Html.Hidden("hdnSelectedValues", Model.DynamicProperties.UrlEncode(), new { id = "hdnSelectedValues" })
            }
        <div id="divDynamicProperty"></div>
    
	    <div>
		    <button type="submit" class="btn btn-primary"><i class="icon-hdd icon-white"></i> Save</button>
            @{String listType = WebUtil.GetQueryStringValue("Type"); listType = listType.IsNullOrEmpty() ? String.Empty : "?Type=" + listType;}
            <a class="btn" href="/Private/Job/List@(listType)"><i class="icon-arrow-left"></i> Back to Job List</a>            
	    </div>
        
        <!--Modal Popup Section Start-->
        <div id="divCategoryDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="divPhotoEditModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                <h4 id="myModalLabel">Select Job Category</h4>
            </div>
            <div class="modal-body jb-inline-label">                
                @Html.SelectableCategoryList(_Categories, true, Model.CategoryID)
                <div class="clear"></div>               
            </div>
            <div class="modal-footer">               
                <button class="btn btn-primary" type="button" onclick="SelectCategory();" value=""><i class="icon-ok icon-white"></i> Select</button>
                <button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i> Cancel</button>
            </div>
        </div>
        <!--Modal Popup Section End-->            
        }
    }
}    
</div>

@section PageHeadSection
{
    <link href="@Url.Content("~/Content/themes/jb-ui/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    @*<style type="text/css">
        #divCategoryDialog{width:800px; margin-left:-400px;}
    </style>*@
}

@section JavaScriptPlaceholder
{
@if (Model != null)
{
    @Html.JavaScript("jquery.validate.min.js", Url)
    @Html.JavaScript("jquery.validate.unobtrusive.min.js", Url)
    @Html.JavaScript("jquery-ui-1.8.11.min.js", Url)
    @Html.Partial("_DatePicker")    
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('#CountryID').change(function () {
                Ajax('/Ajax/GetStatesByCountry'
                , 'countryID=' + $(this).val()
                , BindStates_Success);
            });
            $('#StateID').change(function () {
                Ajax('/Ajax/GetCitiesByState'
                , 'stateID=' + $(this).val()
                , BindCities_Success);
            });
        });

        function BindStates_Success(result) {
            var types = eval(result);
            BindDDL(types, $('#StateID'));
        }
        function BindCities_Success(result) {
            var types = eval(result);
            BindDDL(types, $('#CityID'));
        }
        function BindSkills_Success(result) {
            var types = eval(result);
            BindDDL(types, $('#Skills'));
        }
        function ClearSelection(elementID) {
            $('#' + elementID + ' option').removeAttr('selected');
        }
    </script>
        
    ///Imported JavaScript From Other Project
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('#btnCategorySelection').click(function(){
                $('#divCategoryDialog').modal({ show: true });
            });            
            if (parseInt($('#CategoryID').val()) > 0)
                GetDynamicProperties(@Model.ID, $('#CategoryID').val());            
        });        
        function GetDynamicProperties(jobID, categoryID) {
            var url = '@Url.Action("GetDynamicUI", "Job", new { area = AppConstants.Area.PRIVATE })';
            var data = 'jobID=' + jobID + '&categoryID=' + categoryID;
            var lglValues = $('#hdnSelectedValues').val();
            if(lglValues != undefined && lglValues != null && lglValues.length > 0)
                data += '&lglValues=' + lglValues;
            Ajax(url, data, function(result){
                $('#divDynamicProperty').html(result);
                var form = $('form');
                $(form).removeData('validator');
                $.validator.unobtrusive.parse(form);
            });
        } 
        function GetCategoryBreadCrumb(categoryID){
            var url = '@Url.Action("GetCategoryBreadCrumb", "Ajax", new { area = AppConstants.Area.PUBLIC })';
            var data = 'categoryID=' + categoryID;
            Ajax(url, data, function(result){ $('#spanSelectedCategory').html(result); HideLoadingAnimation(); $('#divCategoryDialog').modal('hide');}); 
        }
        function SelectCategory() {            
            var categoryID = $('#divCategoryDialog input[type=radio]:checked').val();
            if(categoryID == undefined || categoryID == null || categoryID == 0)            
                alert("Please Select a Category."); //$('#dialog').html('Please Select a Category.').dialog('open');            
            else
            {                
                //$(_CategoryDialog).dialog('close');
                ShowLoadingAnimation(false);
                GetDynamicProperties(@Model.ID, categoryID);
                GetCategoryBreadCrumb(categoryID);                
                $('#CategoryID').val(categoryID);
                BindSkills(categoryID);                
            }
        }
        function BindSkills(categoryID)
        {
            var url = '/Ajax/GetSkills?categoryID=' + categoryID;
            AjaxGET(url, BindSkills_Success);
        }
    </script>
}
}



